$(document).ready(function() {
    var fName = $("#first-name");
    var lName = $("#last-name");
    var age = $("#age");

    var country = $("#country");
    var state = $("#state");
    var city = $("#city");
    var street = $("#street");
    var home = $("#home");
    var appartment = $("#appartment");
    var zip = $("#zip");

    var phone = $("#phone");
    var email = $("#email");
    var fax = $("#fax");

//  Start Regular expression
    var namePattern = "^[a-zA-Z][a-zA-Z ,.'-]*$";
    var addressPattern = "^[a-zA-Z ,.'-]+$";
    var homePattern = "^[0-9]";
    var phonePattern = "^[0-9][^a-zA-Z ]";
    var emailPattern = "(([A-Za-z])|(\\d)|([-_.]))+@([A-Za-z])+[.]([A-Za-z]){2,4}";
//  Stop Regular expression

//  Start Reaction to input values
    fName.keyup(function() {
        checkValidate(this, false, namePattern);
    });
    lName.keyup(function() {
        checkValidate(this, false, namePattern);
    });
    age.keyup(function() {
        if (age.val() < 0)
            age.val(18);
    });

    country.keyup(function() {
        checkValidate(this, false, addressPattern);
    });
    state.keyup(function() {
        checkValidate(this, false, addressPattern);
    });
    city.keyup(function() {
        checkValidate(this, false, addressPattern);
    });
    street.keyup(function() {
        checkValidate(this, false, addressPattern);
    });
    home.keyup(function() {
        checkValidate(this, false, homePattern);
    });
    appartment.keyup(function() {
        checkValidate(this, false, homePattern);
    });
    zip.keyup(function() {
        checkValidate(this, false, homePattern);
    });

    phone.keyup(function() {
        checkValidate(this, false, phonePattern);
    });
    email.keyup(function() {
        checkValidate(this, false, emailPattern);
    });
    fax.keyup(function() {
        checkValidate(this, false, phonePattern);
    });
//    Stop Reaction to input values


    $("#create-btn").click(function() {

        checkValidate(fName, true, namePattern);
        checkValidate(lName, true, namePattern);
        checkValidate(country, true, addressPattern);
        checkValidate(state, true, addressPattern);
        checkValidate(city, true, addressPattern);
        checkValidate(street, true, addressPattern);
        checkValidate(home, true, homePattern);
        checkValidate(appartment, true, homePattern);
        checkValidate(zip, true, homePattern);
        checkValidate(phone, true, phonePattern);
        checkValidate(email, true, emailPattern);
        checkValidate(fax, true, phonePattern);

        if (checkValidate(fName, true, namePattern) &&
                checkValidate(lName, true, namePattern) &&
                checkValidate(country, true, addressPattern) &&
                checkValidate(state, true, addressPattern) &&
                checkValidate(city, true, addressPattern) &&
                checkValidate(street, true, addressPattern) &&
                checkValidate(home, true, homePattern) &&
                checkValidate(appartment, true, homePattern) &&
                checkValidate(zip, true, homePattern) &&
                checkValidate(phone, true, phonePattern) &&
                checkValidate(email, true, emailPattern) &&
                checkValidate(fax, true, phonePattern))
        {
            //            Send to servlet information about new customer or modify.
        } else {
            $("#attention-text").html("<li>You entered an invalid value in one of the fields! Please check correctness of all information.</li>");

            $("#attention").slideDown(200);
            
            setTimeout(function() {
                $("#attention").fadeOut(400);
            }, 2500);
        }
    });



    function checkValidate(input, registration, pattern) {
        if ($(input).val() != "" || registration) {
            if (!validate($(input).val(), pattern)) {
                $(input).css("border-color", "#ef3124");
                return false;
            } else {
                $(input).css("border-color", "#79c30c");
                return true;
            }
        } else {
            $(input).css("border-color", "");
        }
    }

    function validate(value, pattern) {
        var reg = new RegExp(pattern);
        return reg.test(value);
    }

});
